#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <lsxintrin.h>

#define DEBUG 0

#if DEBUG
#define LASX_PRINT(RTYPE, element_num, in0)           \
{                                                     \
    RTYPE _tmp0 = (RTYPE)in0;                         \
    int _i = 0;                                       \
    printf("LASX_PRINT %s :",#in0);                   \
    for(_i = 0; _i < element_num; _i++)               \
        printf("%u,",_tmp0[_i]);                      \
    printf("\n");                                     \
}
#endif

/* */
static int lsx_vadd_b(void)
{
	v16i8 v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
	v16i8 v2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

	v16i8 v3 = __lsx_vadd_b(v1, v2);

#if DEBUG
	LASX_PRINT(v16i8, 16, v1);
	LASX_PRINT(v16i8, 16, v2);
	LASX_PRINT(v16i8, 16, v3);
#endif

	//expected value: 2 4 6 8 10 12 14 16 18 20 22 24 25 28 30 32
	if (v3[0] == 2 && v3[1] == 4 && v3[2] == 6 && v3[3] == 8 && v3[4] == 10 &&
			v3[5] == 12 && v3[6] == 14 && v3[7] == 16 && v3[8] == 18 && v3[9] == 20 &&
				v3[10] == 22 && v3[11] == 24 && v3[12] == 26 && v3[13] == 28 && v3[14] == 30 &&
					v3[15] == 32)
		return 0;
	else
		return -1;
}

static int lsx_vavgr_bu(void)
{

    v16u8 v1 = { 1, 2, 3, 4, 5, };
    v16u8 v2 = { 2, 3, 4, 5, 6, };
	v16u8 v3 = __lsx_vavgr_bu(v1, v2);

#if DEBUG
	LASX_PRINT(v16u8, 16, v1);
	LASX_PRINT(v16u8, 16, v2);
	LASX_PRINT(v16u8, 16, v3);
#endif

	//expected value: 2 3 4 5 6 0 0 0 0 0 0 0 0 0 0 0
	if(v3[0] == 2 && v3[1] == 3 && v3[2] == 4 && v3[3] == 5 && v3[4] == 6)
		return 0;
	else
		return -1;
}

int main(int argc, char **argv)
{
	int ret = 0;

	ret += lsx_vadd_b();

	if(!ret) {
		printf("LSX-128: lsx_vadd_b test OK\n");
	} else {
		printf("LSX-128: lsx_vadd_b FAILD\n");
		goto Exit;
	}

	ret += lsx_vavgr_bu();

	if(!ret) {
		printf("LSX-128: lsx_vavgr_bu test OK\n");
	} else {
		printf("LSX-128: lsx_vavgr_bu FAILD\n");
		goto Exit;
	}

	/* test succeed return 0 */
	return ret;

Exit:
	/* test faild return -1 */
	return ret;
}

